﻿/*
LICENCE___________________________________________________________________
    Copyright (C) 2012 Reverdy Sylvain

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
FIN LICENCE______________________________________________________________
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Merise_designer
{
    public partial class Types : Form
    {
        public Types()
        {
            InitializeComponent();
        }

        private int valeurTypeSelectionnee;

        public int Afficher(int idType, Methodes.ObjetsCommun contructor)
        {
            this.dataGridTypes.DataSource = contructor.getContructorType().getAll();
            this.dataGridTypes.Columns[0].Visible = false;
            valeurTypeSelectionnee = idType;

            //Selection par défaut
            for (int i = 0; i < dataGridTypes.Rows.Count; i++)
            {
                if (Convert.ToInt32(dataGridTypes.Rows[i].Cells[0].Value) == idType)
                {
                    this.dataGridTypes.ClearSelection();
                    this.dataGridTypes.CurrentCell = this.dataGridTypes.Rows[i].Cells[1];
                    this.dataGridTypes.Rows[i].Selected = true;
                    break;
                }
            }

            this.StartPosition = FormStartPosition.CenterParent;
            this.ShowDialog();
            return this.valeurTypeSelectionnee;
        }

        private void bt_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridTypes_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (this.dataGridTypes.CurrentRow.Index >= 0)
                {
                    this.valeurTypeSelectionnee = Convert.ToInt32(dataGridTypes.Rows[dataGridTypes.CurrentRow.Index].Cells[0].Value);
                    this.Close();
                }
            }
            catch { }
        }

        public int getValuerChoisie()
        {
            return this.valeurTypeSelectionnee;
        }

        private void dataGridTypes_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Enter)
            {
                dataGridTypes_DoubleClick(null, null);
            }
            if (e.KeyCode == System.Windows.Forms.Keys.Escape)
            {
                bt_Cancel_Click(null, null);
            }
        }

        private void btValid_Click(object sender, EventArgs e)
        {
            dataGridTypes_DoubleClick(null, null);
        }

    }
}
